﻿using Api.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Api.DAL
{
    /// <summary>
    /// 数据访问类:anshenhui
    /// </summary>
    public partial class tz_notify
    {
        private string databaseprefix = "eazy_"; //数据库表名前缀
        public tz_notify()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "tz_notify");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists_I(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "tz_notify");
            strSql.Append(" where instance_id=@instance_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Models.tz_notify model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + databaseprefix + "tz_notify(");
            strSql.Append("instance_id,code,number,title,time1,cause,time2,lianxiren,telephone,subtime,add_time,what)");
            strSql.Append(" values (");
            strSql.Append("@instance_id,@code,@number,@title,@time1,@cause,@time2,@lianxiren,@telephone,@subtime,@add_time,@what)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                    new SqlParameter("@code", SqlDbType.NVarChar,20),
                    new SqlParameter("@number", SqlDbType.NVarChar,20),
                    new SqlParameter("@title", SqlDbType.NVarChar,255),
                    new SqlParameter("@time1", SqlDbType.DateTime),
                    new SqlParameter("@cause", SqlDbType.NVarChar,500),
                    new SqlParameter("@time2", SqlDbType.DateTime),
                    new SqlParameter("@lianxiren", SqlDbType.NVarChar,255),
                    new SqlParameter("@telephone", SqlDbType.NVarChar,20),
                    new SqlParameter("@subtime", SqlDbType.DateTime),
                    new SqlParameter("@add_time", SqlDbType.DateTime),
                    new SqlParameter("@what", SqlDbType.NVarChar,500)};
            parameters[0].Value = model.instance_id;
            parameters[1].Value = model.code;
            parameters[2].Value = model.number;
            parameters[3].Value = model.title;
            parameters[4].Value = model.time1;
            parameters[5].Value = model.cause;
            parameters[6].Value = model.time2;
            parameters[7].Value = model.lianxiren;
            parameters[8].Value = model.telephone;
            parameters[9].Value = model.subtime;
            parameters[10].Value = model.add_time;
            parameters[11].Value = model.what;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Models.tz_notify model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + databaseprefix + "tz_notify set ");
            strSql.Append("instance_id=@instance_id,");
            strSql.Append("code=@code,");
            strSql.Append("number=@number,");
            strSql.Append("title=@title,");
            strSql.Append("time1=@time1,");
            strSql.Append("cause=@cause,");
            strSql.Append("time2=@time2,");
            strSql.Append("lianxiren=@lianxiren,");
            strSql.Append("telephone=@telephone,");
            strSql.Append("subtime=@subtime,");
            strSql.Append("add_time=@add_time,");
            strSql.Append("what=@what");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                    new SqlParameter("@code", SqlDbType.NVarChar,20),
                    new SqlParameter("@number", SqlDbType.NVarChar,20),
                    new SqlParameter("@title", SqlDbType.NVarChar,255),
                    new SqlParameter("@time1", SqlDbType.DateTime),
                    new SqlParameter("@cause", SqlDbType.NVarChar,500),
                    new SqlParameter("@time2", SqlDbType.DateTime),
                    new SqlParameter("@lianxiren", SqlDbType.NVarChar,255),
                    new SqlParameter("@telephone", SqlDbType.NVarChar,20),
                    new SqlParameter("@subtime", SqlDbType.DateTime),
                    new SqlParameter("@add_time", SqlDbType.DateTime),
                    new SqlParameter("@what", SqlDbType.NVarChar,500),
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.instance_id;
            parameters[1].Value = model.code;
            parameters[2].Value = model.number;
            parameters[3].Value = model.title;
            parameters[4].Value = model.time1;
            parameters[5].Value = model.cause;
            parameters[6].Value = model.time2;
            parameters[7].Value = model.lianxiren;
            parameters[8].Value = model.telephone;
            parameters[9].Value = model.subtime;
            parameters[10].Value = model.add_time;
            parameters[11].Value = model.what;
            parameters[12].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + databaseprefix + "tz_notify ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + databaseprefix + "tz_notify ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.tz_notify GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,instance_id,code,number,title,time1,cause,time2,lianxiren,telephone,subtime,add_time,what from " + databaseprefix + "tz_notify ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            Models.tz_notify model = new Models.tz_notify();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.tz_notify GetModel_I(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,instance_id,code,number,title,time1,cause,time2,lianxiren,telephone,subtime,add_time,what from " + databaseprefix + "tz_notify ");
            strSql.Append(" where instance_id=@instance_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            Models.tz_notify model = new Models.tz_notify();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.tz_notify DataRowToModel(DataRow row)
        {
            Models.tz_notify model = new Models.tz_notify();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["instance_id"] != null && row["instance_id"].ToString() != "")
                {
                    model.instance_id = int.Parse(row["instance_id"].ToString());
                }
                if (row["code"] != null)
                {
                    model.code = row["code"].ToString();
                }
                if (row["number"] != null)
                {
                    model.number = row["number"].ToString();
                }
                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }
                if (row["time1"] != null && row["time1"].ToString() != "")
                {
                    model.time1 = DateTime.Parse(row["time1"].ToString());
                }
                if (row["cause"] != null)
                {
                    model.cause = row["cause"].ToString();
                }
                if (row["time2"] != null && row["time2"].ToString() != "")
                {
                    model.time2 = DateTime.Parse(row["time2"].ToString());
                }
                if (row["lianxiren"] != null)
                {
                    model.lianxiren = row["lianxiren"].ToString();
                }
                if (row["telephone"] != null)
                {
                    model.telephone = row["telephone"].ToString();
                }
                if (row["subtime"] != null && row["subtime"].ToString() != "")
                {
                    model.subtime = DateTime.Parse(row["subtime"].ToString());
                }
                if (row["add_time"] != null && row["add_time"].ToString() != "")
                {
                    model.add_time = DateTime.Parse(row["add_time"].ToString());
                }
                if (row["what"] != null)
                {
                    model.what = row["what"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,instance_id,code,number,title,time1,cause,time2,lianxiren,telephone,subtime,add_time,what ");
            strSql.Append(" FROM " + databaseprefix + "tz_notify ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,instance_id,code,number,title,time1,cause,time2,lianxiren,telephone,subtime,add_time,what ");
            strSql.Append(" FROM " + databaseprefix + "tz_notify ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM " + databaseprefix + "tz_notify ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from " + databaseprefix + "tz_notify T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion
    }
}
